﻿/*
Copyright (c) 2009 Vratislav Cermak (vratacermak@gmail.com)
 
This file is part of TeachMe Project. See http://code.google.com/p/teach-me for more information.
 
TeachMe is an open source project. Using of TeachMe is free of charge.
It is distributed under GNU General Public License version 3 (GPLv3).
GPLv3 can be found at http://www.gnu.org/licenses/gpl.html.
 
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE.  See the license for more details.
*/

using System;
using System.Collections.Generic;
using System.IO;
using System.Windows.Forms;
using TeachMe.Application.Exceptions;
using TeachMe.Application.Properties;

namespace TeachMe.Application
{
    public class PrioritiesStore
    {
        private readonly Dictionary<Guid, string> _priorities = new Dictionary<Guid, string>();
        private readonly string _dirName;

        public PrioritiesStore(string dirName)
        {
            _dirName = dirName;

            Reload();
        }

        public Dictionary<Guid, string> Priorities
        {
            get { return _priorities; }
        }

        public string DirName
        {
            get { return _dirName; }
        }

        private void Reload()
        {
            Priorities.Clear();

            string[] files = Directory.GetFiles(DirName, "*.dus");
            bool failed = false;

            foreach (string file in files)
            {
                XmlEditor editor;

                try
                {
                    editor = XmlEditor.Load(file);
                }
                catch (XmlEditorException e)
                {
                    Logging.Error(GetType(), e, "Priorities could not be loaded.");

                    failed = true;
                    
                    continue;
                }

                Priorities.Add(new Guid(editor.GetAttributeValue("dictionaryId")), file);
            }

            if (failed)
            {
                MessageBox.Show(Resources.PrioritiesLoadError, Resources.MessageBoxCaption, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        public void Create(string path, Guid dictionaryId)
        {
            XmlEditor editor;

            try
            {
                editor = XmlEditor.Create(path, "Priorities");
            }
            catch (XmlEditorException exception)
            {
                Logging.Error(GetType(), exception, "Priorities could not be created.");

                throw new DictionaryException("Priorities could not be created.", exception);
            }

            editor.SetAttributeValue("dictionaryId", dictionaryId.ToString());

            using (editor.AddNode("Entries"))
            {
                //
            }

            try
            {
                editor.Save();
            }
            catch (XmlEditorException exception)
            {
                Logging.Error(GetType(), exception, "Priorities could not be saved.");

                throw new DictionaryException("Priorities could not be saved.", exception);
            }

            _priorities.Add(dictionaryId, path);
        }
    }
}
